00001 // Emacs Mode Line: -*- Mode:c++;-*- 00002 /* 00003 * Copyright (c) 2013 Battelle Memorial Institute 00004 * Licensed under modified BSD License. A copy of this license can be found 00005 * in the LICENSE file in the top level directory of this distribution. 00006 */ 00007 // ------------------------------------------------------------- 00008 /** 00009 * @file index_hash.hpp 00010 * @author Bruce Palmer 00011 * @date 2014-06-24 09:25:03 d3g293 00012 * 00013 * @brief 00014 * This is a utility that is designed to provide a relatively efficient way of 00015 * mapping between different sets of indexes in a distributed way. Note that all 00016 * these operations are collective 00017 * 00018 */ 00019 00020 // ------------------------------------------------------------- 00021 00022 #ifndef _hash_map_hpp_ 00023 #define _hash_map_hpp_ 00024 00025 #include <map> 00026 #include <vector> 00027 #include "gridpack/parallel/communicator.hpp" 00028 00029 namespace gridpack { 00030 namespace hash_map { 00031 00032 // ------------------------------------------------------------- 00033 // class GlobalIndexHashMap 00034 // ------------------------------------------------------------- 00035 class GlobalIndexHashMap { 00036 public: 00037 00038 // Default constructor 00039 GlobalIndexHashMap(const parallel::Communicator &comm); 00040 00041 // Default destructor 00042 ~GlobalIndexHashMap(void); 00043 00044 // add key-value pairs to hash map where key is singe integer 00045 // @param pairs list of key-value pairs where both keys and values are 00046 // integers 00047 void addPairs(std::vector<std::pair<int,int> > &pairs); 00048 00049 // add key-value pairs to hash map where key is another index pair of integers 00050 // @param pairs list of key-value pairs where key is a pair of integers and 00051 // value is a single integer 00052 void addPairs(std::vector<std::pair<std::pair<int,int>,int> > &pairs); 00053 00054 // get values corresponding to a list of keys from the hash map where key is a 00055 // single integer 00056 // @param keys list of integer keys 00057 // @param values returned list of values corresponding to the list of keys 00058 void getValues(std::vector<int> &keys, std::vector<int> &values); 00059 00060 // get values corresponding to a list of keys from the hash map where key is a 00061 // pair of integers 00062 // @param keys list of integer-pair keys 00063 // @param values returned list of values corresponding to the list of keys 00064 void getValues(std::vector<std::pair<int,int> > &keys, std::vector<int> &values); 00065 00066 private: 00067 00068 // hash function for indices. Maps the value of key into the interval 00069 // [0,p_nprocs-1] where p_nprocs is the total number of processors 00070 // @param key input value of key 00071 // @return number between 0 and p_nprocs-1 00072 int hashValue(int key); 00073 00074 // hash function for index pairs. Maps the value of key into the interval 00075 // [0,p_nprocs-1] where p_nprocs is the total number of processors 00076 // @param key input value of key 00077 // @return number between 0 and nprocs-1 00078 int pairHashValue(std::pair<int,int> key); 00079 00080 int p_nprocs; 00081 int p_me; 00082 int p_GAgrp; 00083 00084 MPI_Comm p_comm; 00085 00086 std::multimap<int, int> p_umap; 00087 00088 std::multimap<std::pair<int,int>, int> p_pmap; 00089 }; 00090 00091 00092 } // namespace gridpack 00093 } // namespace hash_map 00094 00095 #endif 00096